"""
Case Type   : datakit录制回放工具
Case Name   : 创建录制回放任务时新增用户
Create At   : 2025/3/27
Owner       : @lonely-dance
Description :
    1.导入服务器
    2.导入实例
    3.新增用户
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.datakit_api.DataMigration import DataMigrationApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.mysql_node = Node('mysql5')
        self.primary_node = Node('PrimaryDbUser')
        self.primary_root_node = Node('PrimaryRoot')
        self.node_list = [self.mysql_node, self.primary_node]
        self.com_sh = CommonSH('PrimaryDbUser')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.datakit_sh = CommonDatakit(self.token)

    def test_transcribe_replay(self):
        text = '----step1:添加服务器 expect:成功----'
        self.log.info(text)
        self.datakit_sh.import_host(self.node_list)

        text = '----step2:添加实例 expect:成功----'
        self.log.info(text)
        self.datakit_sh.import_db(self.node_list)

        text = '----step3:新增用户 expect:成功----'
        self.log.info(text)
        self.log.info('----测试联通性----')
        data = {"privateIp": f"{self.primary_root_node.ssh_host}", "publicIp": f"{self.primary_root_node.ssh_host}",
                "port": 22, "password": f"{self.primary_root_node.ssh_password}",
                "username": f"{self.primary_root_node.ssh_user}"}
        self.log.info(data)
        response = self.com_api.post_host_ping(data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '测试联通性失败')

        self.log.info('----增加用户----')
        response = self.com_api.get_host_page(name=f"{self.primary_root_node.ssh_host}")
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '服务器不存在')
        host_id = response.json()['rows'][0]['hostId']
        data = {"hostId": f"{host_id}", "username": f"{self.primary_root_node.ssh_user}",
                "password": f"{self.primary_root_node.ssh_password}"}
        self.log.info(data)
        response = self.com_api.post_hostUser(data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        self.log.info('----查询用户是否新增成功----')
        response = self.com_api.get_hostUser_page(host_id)
        self.log.info(response.json())
        self.assertIn(self.primary_root_node.ssh_user, response.text, '执行失败' + text)

    def tearDown(self):
        text = '----step3:清理环境----'
        self.log.info(text)
        self.log.info('----删除服务器----')
        self.datakit_sh.delete_host()

        self.log.info('----删除实例----')
        self.datakit_sh.delete_db()

        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
